#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
快速测试Cloudflare Tunnel连接
"""

import asyncio
import websockets
import json
import sys

async def quick_test(url):
    try:
        # 转换为WebSocket URL
        if url.startswith('https://'):
            ws_url = url.replace('https://', 'wss://')
        else:
            ws_url = url.replace('http://', 'ws://')
        
        print(f"🔗 测试连接: {ws_url}")
        
        async with websockets.connect(ws_url, timeout=10) as websocket:
            print("✅ 连接成功!")
            
            # 接收服务器消息
            response = await websocket.recv()
            data = json.loads(response)
            
            if data.get('type') == 'CONNECTION_ACK':
                print("✅ 服务器响应正常")
                print("🎉 Cloudflare Tunnel 配置成功!")
                print("🌐 现在支持100+用户跨网络访问")
                return True
                
        return False
        
    except Exception as e:
        print(f"❌ 测试失败: {e}")
        return False

if __name__ == "__main__":
    if len(sys.argv) < 2:
        url = input("请输入Tunnel地址 (如 https://abc123.trycloudflare.com): ")
    else:
        url = sys.argv[1]
    
    result = asyncio.run(quick_test(url))
    
    if result:
        print("\n✅ 测试通过! 现在可以进行跨网络数据传输")
    else:
        print("\n❌ 测试失败，请检查配置")
